Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@sindresorhus/chunkify
Advanced tools
Split an iterable into evenly sized chunks
npm install @sindresorhus/chunkify
import chunkify from '@sindresorhus/chunkify';
console.log([...chunkify([1, 2, 3, 4], 2)]);
//=> [[1, 2], [3, 4]]
console.log([...chunkify([1, 2, 3, 4], 3)]);
//=> [[1, 2, 3], [4]]
Returns an iterable with the chunks. The last chunk could be smaller.
Type: Iterable
(for example, Array
)
The iterable to chunkify.
Type: number
(integer)
Minimum: 1
The size of the chunks.
When dealing with large datasets, breaking data into manageable chunks can optimize the batch processing tasks.
import chunkify from '@sindresorhus/chunkify';
const largeDataSet = [...Array(1000).keys()];
const chunkedData = chunkify(largeDataSet, 50);
for (const chunk of chunkedData) {
processBatch(chunk);
}
Dividing data into chunks can be useful in parallel processing to distribute workload evenly across different threads or workers.
import {Worker} from 'node:worker_threads';
import chunkify from '@sindresorhus/chunkify';
const data = [/* some large dataset */];
const chunkedData = chunkify(data, 20);
for (const [index, chunk] of chunkedData.entries()) {
const worker = new Worker('./worker.js', {
workerData: {
chunk,
index
}
});
}
Splitting a large number of network requests into chunks can help in managing the load on the network and preventing rate limiting.
import chunkify from '@sindresorhus/chunkify';
const urls = [/* Array of URLs */];
const chunkedUrls = chunkify(urls, 10);
for (const chunk of chunkedUrls) {
await Promise.all(chunk.map(url => fetch(url)));
}
FAQs
Split an iterable into evenly sized chunks
We found that @sindresorhus/chunkify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.